import { size, sp, Vec3, view } from "cc";

export default class Common {
    private static _instance: Common;
    public static get instance(): Common {
        if (!this._instance) {
            this._instance = new Common();
        }
        return this._instance;
    };

    playSpine(spine: sp.Skeleton, name: string, loop: boolean, callBefore: Function = null, callAfter: Function = null) {
        // console.log(` spine ${spine.node.name}; ani ${name}`);
        if (callBefore) {
            callBefore && callBefore();
        }
        if (callAfter) {
            spine.setCompleteListener((trackEntry) => {
                spine.setCompleteListener(null);
                if (trackEntry.animation.name === name) {
                    callAfter();
                }
            });
        }
        spine.findAnimation(name) && spine.setAnimation(0, name, loop);
    }

    setSpineMix(spine: sp.Skeleton, names: string[], interval: number) {
        let length = names.length;
        for (let i = 0; i < length; i++) {
            let nameA = names[i];
            for (let j = 0; j < length; j++) {
                let nameB = names[j];
                if (nameA == nameB) {
                    continue;
                }
                spine.setMix(nameA, nameB, interval);
            }
        }
    };

    getScreenSize() {
        const visibleSize = view.getVisibleSize();
        const designSize = view.getDesignResolutionSize();
        return size(designSize.width, visibleSize.height);
    }

    getEnergyY(){
        let size = this.getScreenSize();
        return size.height * 0.5 - 100;
    }

    getTimerY(){
        let size = this.getScreenSize();
        return size.height * 0.5 - 70;
    }

    getBottomY(){
        let size = this.getScreenSize();
        return -size.height * 0.5 - 200;
    }

    getHeartX(){
        let size = this.getScreenSize();
        return -size.width * 0.5 + 69;
    }

    // 二次贝塞尔曲线计算
    quadraticBezier(p0: Vec3, p1: Vec3, p2: Vec3, t: number): Vec3 {
        const x = (1 - t) * (1 - t) * p0.x + 2 * t * (1 - t) * p1.x + t * t * p2.x;
        const y = (1 - t) * (1 - t) * p0.y + 2 * t * (1 - t) * p1.y + t * t * p2.y;
        return new Vec3(x, y, p0.z);
    }

    // 三次贝塞尔曲线计算
    cubicBezier(p0: Vec3, p1: Vec3, p2: Vec3, p3: Vec3, t: number): Vec3 {
        const t1 = 1 - t;
        const x = t1 * t1 * t1 * p0.x + 3 * t1 * t1 * t * p1.x + 3 * t1 * t * t * p2.x + t * t * t * p3.x;
        const y = t1 * t1 * t1 * p0.y + 3 * t1 * t1 * t * p1.y + 3 * t1 * t * t * p2.y + t * t * t * p3.y;
        return new Vec3(x, y, p0.z);
    }

    /**
     * 深拷贝
     * @param {*} Obj 
     * @returns 
     */
    clone(Obj: any) {
        var buf;
        if (Obj instanceof Array) {
            buf = [];
            var i = Obj.length;
            while (i--) {
                buf[i] = this.clone(Obj[i]);
            }
            return buf;
        }
        else if (Obj instanceof Object) {
            buf = {};
            for (var k in Obj) {
                buf[k] = this.clone(Obj[k]);
            }
            return buf;
        } else {
            return Obj;
        }
    };

    log(...params: any[]): void {
        try {
            var logContent = [];
            for (var i in params) {
                var content = params[i];
                if (typeof (params[i]) == "object") {
                    content = JSON.stringify(params[i]);
                }
                logContent.push(content);
            }
            console.log("--输出：" + logContent.join(""));
        }
        catch (e) {
            console.log("--输出：" + logContent.join(""));
        }
    };
};